package com.eshop.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.text.DecimalFormat;

public class DoubleSerializer extends JsonSerializer<Double>
{
    public void serialize(final Double value, final JsonGenerator gen, final SerializerProvider serializerProvider) throws IOException {
        if (value != null && !"".equals(value)) {
            // 只保留两位小数
            final DecimalFormat df2 = new DecimalFormat("0.00");
            gen.writeString(df2.format(value));
        }
        else {
            gen.writeString(value + "");
        }
    }
}
